package ystp.service;


import org.springframework.stereotype.Service;
import ystp.dao.YsuserMapper;
import ystp.model.Ysuser;
import ystp.model.YsuserExample;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class YsuserService {
    @Resource
    YsuserMapper ddMapper;

    public boolean  checkAccountByuserid(String ppaccount,String ppUserId) {
        YsuserExample mmExample=new YsuserExample();
        mmExample.createCriteria().andAccountEqualTo(ppaccount).andUseridNotEqualTo(ppUserId);
        return ddMapper.selectByExample(mmExample).size()>0?true:false;
    }

    public boolean  checkAccount(String ppaccount) {
        YsuserExample mmExample=new YsuserExample();
        mmExample.createCriteria().andAccountEqualTo(ppaccount);
        return ddMapper.selectByExample(mmExample).size()>0?true:false;
    }

    public List<Map<String, Object>> findVaild(String ppUsername, int ppPageIndex, int ppPageSize){

        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;

        return ddMapper.findRenyuanByisvalid(ppUsername,mmStartIndex, ppPageSize);

    }

    public Integer getCountByisvalid(String ppUsername) {
        Object mmCount = ddMapper.getCountByisvalid(ppUsername);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Ysuser checkLogin(String ppLoginName, String ppLoginPassword) {
        YsuserExample example = new YsuserExample();
        YsuserExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(ppLoginName).andPasswordEqualTo(ppLoginPassword);
        return ddMapper.selectByExample(example).size() > 0 ? ddMapper.selectByExample(example).get(0) : null;
    }

    public Ysuser findOne(String ppuserId){
        return ddMapper.selectByPrimaryKey(ppuserId);
    }

    public List<Ysuser> findAll(String ppuserName){
        YsuserExample mmExample=new YsuserExample();
        YsuserExample.Criteria criteria = mmExample.createCriteria();
        criteria.andUsernameLike(ppuserName);
        return ddMapper.selectByExample(mmExample).size()>0? ddMapper.selectByExample(mmExample):
                null;

    }

    public boolean modify(Ysuser ppYsuser) {
        return ddMapper.updateByPrimaryKeySelective(ppYsuser)==1;
    }

    public boolean add(Ysuser ppYsuser) {
        return ddMapper.insert(ppYsuser)==1;
    }

    public boolean delete(String ppuserId){
        return ddMapper.deleteByPrimaryKey(ppuserId)==1;
    }

    public boolean isexistByAccount(String account){
        YsuserExample mmExample=new YsuserExample();
        YsuserExample.Criteria criteria=mmExample.createCriteria();
        criteria.andAccountEqualTo(account);
        return  ddMapper.selectByExample(mmExample).size()>0;
    }
    public List<Ysuser> findByAccount(String account){
        YsuserExample mmExample=new YsuserExample();
        YsuserExample.Criteria criteria=mmExample.createCriteria();
        criteria.andAccountEqualTo(account);
        return ddMapper.selectByExample(mmExample);
    }

}
